import { _decorator, Component, Node, EventHandler, Enum, log } from 'cc';
import { DEV, EDITOR } from 'cc/env';
import { KeyActionHandler } from './KeyActionHandler';
import { KeyAction, KeyActionInfo, KeyActionType } from './KeyActionDefine';
import { KeyCodeHandler } from '../base/KeyCodeHandler';
import { InputKeyCode } from '../../core/InputConfig';
import GameInputMgr from '../../core/GameInputMgr';
const { ccclass, property, type, executeInEditMode } = _decorator;

@ccclass('SingleActionHandler')
export class SingleActionHandler extends KeyActionHandler {


    keyCodes: InputKeyCode[] = []
    @property({ tooltip: DEV && "键盘激活时时的默认选中项" })
    @property({ type: [EventHandler] })
    handler: EventHandler[] = [];

    @property({ type: Enum(KeyActionType), override: true })
    set actionType(value: KeyActionType) {
        console.log(' SingleActionHandler ')
        if (this._actionType === value || value == KeyActionType.none) {
            return;
        }

        this._actionType = value;
        this.initKeyCodes()
        this.handler.length = 0;
        for (let i = 0; i < this.keyCodes.length; i++) {
            this.handler[i] = new EventHandler()
        }
    }

    get actionType() {
        return this._actionType;
    }

    @property({ tooltip: DEV && "是否监听键盘的抬起和鼠标的按下事件" })
    @property
    hasUpEvent: boolean = false;

    start() {
        this.initHandlers()
        super.start();
    }

    reset() {
        this.initKeyCodes();
        for (let i = 0; i < this.handler.length; i++) {
            let kcHandler = this.kcHandlers[i]
            if (kcHandler) {
                kcHandler.isContinuous = this.isContinuous;
                kcHandler.continuousDuration = this.continuousDuration;
                kcHandler.keyCode = this.keyCodes[i];
                if (this.model) {
                    kcHandler.soundEventId = this.model.soundEventId;
                }
            }
        }
    }

    get soundEventId() {
        if (this.model) {
            return this.model.soundEventId;
        }
        return 0;
    }

    protected initKeyCodes() {
        this.keyCodes.length = 0;
        let data: KeyAction = KeyActionInfo[KeyActionType[this._actionType]]
        if (!data) {
            log('未设置行为 ', this.actionType, this.node.name)
        } else {
            let list = data.keyCodes;
            for (let j = 0; j < list.length; j++) {
                this.keyCodes.push(GameInputMgr.instance().converter.getKeyValueByKeyName(list[j]));
            }
        }

    }

    initHandlers() {
        if (EDITOR) {
            this.removeAllKeyCodeHandler();
            return;
        }
        // Debug.log(' SingleActionHandler this.node.name initHandlers ', this.node.name, this.kcHandlers.length)
        if (this.kcHandlers.length > 0) {
            // Debug.log(' this.kcHandlers count > 0 ', this.node.name)
            return;
        }
        this.initKeyCodes()

        // Debug.log('SingleActionHandler  this.keyCodes ', this.keyCodes, this.handler)
        for (let i = 0; i < this.handler.length; i++) {
            let kcHandler = this.node.addComponent(KeyCodeHandler)
            kcHandler.isContinuous = this.isContinuous;
            kcHandler.continuousDuration = this.continuousDuration;
            kcHandler.keyCode = this.keyCodes[i];
            kcHandler.handler = this.handler[i]
            kcHandler.hasUpEvent = this.hasUpEvent;
            kcHandler.isBelongKeyBoard = this.isBelongKeyBoard;
            if (this.model) {
                kcHandler.soundEventId = this.model.soundEventId;
            }

            this.kcHandlers.push(kcHandler);
        }
    }

}

